package com.terren.statistic.execute.excel;

import java.io.File;
import java.util.List;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.terren.statistic.common.util.ExcelUtil;
import com.terren.statistic.db.entity.TopicStatis;

public class TopicExcel {
	
	private static String[] TITLE = { "话题", "配置", "官媒UV", "门户UV" };
	/**
	 * 
	 * @param dataList
	 * @param name
	 */
	public String createExcel(List<TopicStatis> dataList, String name, String path) throws Exception {
		//标题  来源站点 发布时间  PV UV 文章内容  URL

		/** 创建Excel的工作书册 Workbook,对应到一个excel文档 **/
		XSSFWorkbook excelBook = new XSSFWorkbook();

		/** 创建Excel的工作sheet,对应到一个excel文档的tab **/
		XSSFSheet sheet = excelBook.createSheet(name);

		/** 设置excel每列宽度 **/
		sheet.setColumnWidth(0, 20*256);
		sheet.setColumnWidth(1, 15*256);
		sheet.setColumnWidth(2, 12*256);
		sheet.setColumnWidth(3, 12*256);

		/** 创建单元格样式，左对齐 **/
		XSSFCellStyle leftSt = excelBook.createCellStyle();
		leftSt.setAlignment(HorizontalAlignment.LEFT);
		leftSt.setVerticalAlignment(VerticalAlignment.CENTER);
		
		/** 创建单元格样式，右对齐 **/
		XSSFCellStyle rightSt = excelBook.createCellStyle();
		rightSt.setAlignment(HorizontalAlignment.RIGHT);
		rightSt.setVerticalAlignment(VerticalAlignment.CENTER);

		/** ============================== 第一行 标题 ============================== **/
		/** 创建Excel的sheet的一行 **/
		XSSFRow row = sheet.createRow(0);

		/** 创建一个Excel的单元格 **/
		XSSFRichTextString stringValue = null;

		XSSFCell cell = row.createCell(0);
		cell.setCellStyle(leftSt);
		stringValue = new XSSFRichTextString(TITLE[0]);
		cell.setCellValue(stringValue);

		cell = row.createCell(1);
		cell.setCellStyle(leftSt);
		stringValue = new XSSFRichTextString(TITLE[1]);
		cell.setCellValue(stringValue);

		cell = row.createCell(2);
		cell.setCellStyle(leftSt);
		stringValue = new XSSFRichTextString(TITLE[2]);
		cell.setCellValue(stringValue);

		cell = row.createCell(3);
		cell.setCellStyle(leftSt);
		stringValue = new XSSFRichTextString(TITLE[3]);
		cell.setCellValue(stringValue);
		

		/** ============================== 第二行 数据 ============================== **/
		if(dataList == null || dataList.isEmpty()){//无结果
			/** 新的一行 **/
			row = sheet.createRow(1);
			/** ==================== 列 ==================== **/
			cell = row.createCell(0);
			stringValue = new XSSFRichTextString("无结果");
			cell.setCellValue(stringValue);
		}else{
			int number = 0;
			for(TopicStatis doc : dataList){
				number ++;
				/** 新的一行 **/
				row = sheet.createRow(number);

				/** ==================== 列 ==================== **/
				/** 1列 title  **/
				cell = row.createCell(0);
				cell.setCellStyle(leftSt);
				stringValue = new XSSFRichTextString(doc.getTopic());
				cell.setCellValue(stringValue);

				/** 2列 网站来源 **/
				cell = row.createCell(1);
				cell.setCellStyle(leftSt);
				stringValue = new XSSFRichTextString(doc.getKwConfig());
				cell.setCellValue(stringValue);

				/** 3列 发布时间 **/
				cell = row.createCell(2);
				cell.setCellStyle(rightSt);
				cell.setCellValue(doc.getUvOfcType0());
				
				/** 4列 PV **/
				cell = row.createCell(3);
				cell.setCellStyle(rightSt);
				cell.setCellValue(doc.getUvOfcType1());
			}
		}

		ExcelUtil eu = new ExcelUtil();
		File excelFile = eu.writeFile(excelBook, name, path);
		String excelPath = excelFile.getAbsolutePath();
		return excelPath;
	}
}
